package softstarter.gui.subareas.commandline;

import java.awt.Graphics;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JComboBox;

import softstarter.launching.RunnableItem;

public class CustomJComboBox extends JComboBox {

	private List<RunnableItem> topHits;
	private boolean topHitsUpdated;
	
	public CustomJComboBox() {
		topHits = new ArrayList<RunnableItem>();
		topHitsUpdated = true;
	}
	
	public void updateHitList(List<RunnableItem> topHits) {
		this.topHits = topHits;
		topHitsUpdated = true;
	}
	
	public void paint(Graphics g) {
		
		if(topHitsUpdated) {
			synchronized (this) {
				
				System.out.println("Updating hit list");
				
				String firstElement = this.getEditor().getItem().toString();
	
				this.removeAllItems();
				
				for(RunnableItem runnable : topHits) {
					this.addItem(runnable);
				}
				
				this.getEditor().setItem(firstElement);
				
//				this.revalidate();
//				
				this.showPopup();
	
				topHitsUpdated = false;
			}
		}
		
		super.paint(g);
	}
	
}
